package dao

import (
	"echo-utils/global"
	"echo-utils/internal/model"

	"xorm.io/xorm"
)

type SystemDao struct {
	engine *xorm.Engine
}

func (s *SystemDao) SetPath(path string) []model.Setting {
	ltp := s.find(path)
	if len(ltp) == 0 {
		s.insert(path)
	}
	return s.findAll()
}

func (s *SystemDao) DelPath(id int64) []model.Setting {
	s.delete(id)
	return s.findAll()
}

func (s *SystemDao) Paths() []model.Setting {
	return s.findAll()
}

func (s *SystemDao) findAll() []model.Setting {
	list := make([]model.Setting, 0)
	s.engine.Find(&list)
	return list
}

func (s *SystemDao) find(path string) []model.Setting {
	list := make([]model.Setting, 0)
	s.engine.Where("path=?", path).Find(&list)
	return list
}

func (s *SystemDao) insert(path string) {
	sets := &model.Setting{}
	sets.Path = path
	s.engine.Insert(sets)
}

func (s *SystemDao) delete(id int64) []model.Setting {
	sets := new(model.Setting)
	s.engine.ID(id).Delete(sets)
	return s.findAll()
}

func NewSystemDao() *SystemDao {
	return &SystemDao{
		engine: global.DBEngine,
	}
}
